import React, { Component } from 'react';
import context from "../uilt/context"
import { category_subcate } from "../uilt/api"
import { withRouter } from 'react-router-dom';
class MyRight extends Component {
  constructor(props) {
    super(props);
    this.state = {
      navid: 281435,
      catelist: [],
      currentIndex: 0,
    }
  }
  componentDidMount() {
   
    context.addListener("savaid", (id, index) => {
      this.setState({
        navid: id,
      }, () => {
        // 发送二级菜单请求
        category_subcate({ id: this.state.navid }).then((res) => {
          this.setState({
            catelist: res.data.subcate.subcategory
          })
        })
      })
    })
    category_subcate({ id: this.state.navid }).then((res) => {
      this.setState({
        catelist: res.data.subcate.subcategory,
 
      })
    })
    
  }
  handlerouter(id){
    this.props.history.push({ 
      // search 属性会在 pathname 属性填写的路径后默认补 一个问号？，
      // 需要显示在地址栏的信息可以写在 search 属性中
      pathname: `/index/goods`, 
      search: `id=${id}`, 
    })
  }
  render() {
    return (
      <div className='cate'>
        {
         this.state.catelist.length>0&& this.state.catelist.map((item, index) => {
            return (
              <div className="caterigth" key={index}>
                <div className="top">{item.name}</div>
              <div className="catemain">
              {
                item.subNavs.map((item,index)=>{
                  return (
                    <div className="listitem" key={index} onClick={()=>{this.handlerouter(item.id)}}>
                    <img src={item.pic} alt="" />
                 <div className="name">{item.name}</div>
                  </div> 
                  )
                })
               }
              </div>
                  </div>
            )
          })
  }
      </div>
    );
  }
}

export default withRouter(MyRight);